package leetcode;

import java.util.ArrayList;
import java.util.List;

class Solution_39 {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> temp = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backtracking(candidates,target,0,0);
        return result;
    }
    public void backtracking(int[] candidates,int target,int start, int sum){
        if (sum > target){
            return;
        }
        if (sum == target){
            result.add(new ArrayList<>(temp));
            return;
        }
        for (int i = start; i < candidates.length; i++) {
            temp.add(candidates[i]);
            backtracking(candidates,target,i,sum + candidates[i]);
            temp.removeLast();
        }
    }
}